package com.guigu.tankai.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.guigu.aop.OpLog;
import com.guigu.liuzhireng.pojo.PageVo;
import com.guigu.tankai.pojo.Dfile;
import com.guigu.tankai.pojo.Dmodule;
import com.guigu.tankai.service.DfileService;
import com.guigu.tankai.service.DmoduleService;
import com.guigu.tankai.service.DmoduledetailsService;
import com.guigu.util.service.IDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("Dmodule.do")
public class DmoduleController {

    @Autowired
    DmoduleService dmoduleService;
    @Autowired
    DmoduledetailsService dmoduledetailsService;
    @Autowired
    IDService idService;
    @Autowired
    DfileService dfileService;

    @RequestMapping("DmoduleAdd")
    @ResponseBody
    @OpLog(opModule = "物料组成设计",operType = "添加产品",operDesc = "此方法用于添加产品到两个表的数据")
    public String DmoduleAdd(String lists,String plist) {
        List<Dmodule> list = new Gson().fromJson(lists,new TypeToken<List<Dmodule>>(){}.getType());
        String ebomID = idService.EbomID();
        int zhi = 0;
        for (Dmodule dmodule : list) {
            zhi += dmodule.getCostpricesum();
        }
        for (Dmodule dmodule : list) {
            dmodule.setDesignid(ebomID);
            dmodule.setCostpricesum(zhi);
        }
        Dfile dfile = new Gson().fromJson(plist, Dfile.class);
        dfileService.updatecha(dfile);
        Dmodule dmodule = new Dmodule();
        dmodule.setDesignid(ebomID);
        dmodule.setProductid(dfile.getProductid());
        dmodule.setProductname(dfile.getProductname());
        dmodule.setFirstkindid(dfile.getFirstkindid());
        dmodule.setFirstkindname(dfile.getFirstkindname());
        dmodule.setSecondkindid(dfile.getSecondkindid());
        dmodule.setSecondkindname(dfile.getSecondkindname());
        dmodule.setThirdkindid(dfile.getThirdkindid());
        dmodule.setThirdkindname(dfile.getThirdkindname());
        dmodule.setChecktag("S001-0");
        dmodule.setChangetag("B002-0");
        dmodule.setDesigner(list.get(0).getDesigner());
        dmodule.setModuledescribe(list.get(0).getModuledescribe());
        dmodule.setCostpricesum(zhi);
        dmodule.setRegister(list.get(0).getRegister());
        dmodule.setRegistertime(list.get(0).getRegistertime());
        int dmid = dmoduleService.insert_TK(dmodule);
        for (Dmodule dm : list) {
            dm.setId(dmid);
        }
        dmoduledetailsService.insert_TK(list);
        return "/tan_kai/DModuleAdd.html";
    }
    @RequestMapping("/pagewtg")
    @ResponseBody
    public PageVo<Dfile> pagewtg(@RequestBody PageVo<Dfile> f){
        return dmoduleService.pagewtg(f.getNum(),f.getSize(),f.getObj());
    }
    @RequestMapping("/pageyitg")
    @ResponseBody
    public PageVo<Dmodule> pageyitg(@RequestBody PageVo<Dmodule> f){
        return dmoduleService.pageyitg(f.getNum(),f.getSize(),f.getObj());
    }
    @RequestMapping("/pagewgg")
    @ResponseBody
    public PageVo<Dmodule> pagewgg(@RequestBody PageVo<Dmodule> f){
        return dmoduleService.pagewgg(f.getNum(),f.getSize(),f.getObj());
    }
    @RequestMapping("upfliewtg")
    @OpLog(opModule = "物料组成设计",operType = "修改产品",operDesc = "此方法用于修改产品为未通过")
    public String  upfliewtg(Dmodule dfile){
        dmoduleService.updateByPrimaryKey(dfile);
        return  "/tan_kai/Dmwtg.html";
    }
    @RequestMapping("upDmgg")
    @OpLog(opModule = "物料组成设计",operType = "修改产品",operDesc = "此方法用于修改产品为已审核和未设计")
    public String  upDmgg(Dmodule dfile){
        dmoduleService.updateByPrimaryKeySelective(dfile);
        return  "/tan_kai/Dmwtg.html";
    }
    // limeng
    @RequestMapping("/showDmo")
    public List<Dmodule> showDmo(){
        return dmoduleService.showDmo();
    }
}
